import Vue from 'vue'
import VueRouter from 'vue-router'
// import LorR from '../components/LorR.vue'
// import Login from '../components/Login.vue'
// import Register from '../components/Register.vue'
// import MangerHome from '../components/MangerHome.vue'
// 路由懒加载
const LorR = () => import(/* webpackChunkName: "LorR_Login_Register_MangerHome"*/ '../components/LorR.vue')
const Login = () => import(/* webpackChunkName: "LorR_Login_Register_MangerHome"*/ '../components/Login.vue')
const Register = () => import(/* webpackChunkName: "LorR_Login_Register_MangerHome"*/ '../components/Register.vue')
const MangerHome = () => import(/* webpackChunkName: "LorR_Login_Register_MangerHome"*/ '../components/MangerHome.vue')

// import MangerWelcome from '../components/MangerWelcome.vue'
// import UserManger from '../components/user_manger/UserManger.vue'
// import RightManger from '../components/power_manger/RightManger.vue'
// import RolesManger from '../components/power_manger/RolesManger.vue'
const MangerWelcome = () => import(/* webpackChunkName: "MangerWelcome_UserManger_RightManger_RolesManger"*/ '../components/MangerWelcome.vue')
const UserManger = () => import(/* webpackChunkName: "MangerWelcome_UserManger_RightManger_RolesManger"*/ '../components/user_manger/UserManger.vue')
const RightManger = () => import(/* webpackChunkName: "MangerWelcome_UserManger_RightManger_RolesManger"*/ '../components/power_manger/RightManger.vue')
const RolesManger = () => import(/* webpackChunkName: "MangerWelcome_UserManger_RightManger_RolesManger"*/ '../components/power_manger/RolesManger.vue')

// import GoodsCate from '../components/goods_manger/GoodsCate.vue'
// import GoodsParams from '../components/goods_manger/GoodsParams.vue'
// import GoodsList from '../components/goods_manger/GoodsList.vue'
// import GoodAdd from '../components/goods_manger/GoodAdd.vue'
const GoodsCate = () => import(/* webpackChunkName: "GoodsCate_GoodsParams_GoodsList_GoodAdd"*/ '../components/goods_manger/GoodsCate.vue')
const GoodsParams = () => import(/* webpackChunkName: "GoodsCate_GoodsParams_GoodsList_GoodAdd"*/ '../components/goods_manger/GoodsParams.vue')
const GoodsList = () => import(/* webpackChunkName: "GoodsCate_GoodsParams_GoodsList_GoodAdd"*/ '../components/goods_manger/GoodsList.vue')
const GoodAdd = () => import(/* webpackChunkName: "GoodsCate_GoodsParams_GoodsList_GoodAdd"*/ '../components/goods_manger/GoodAdd.vue')

// import GoodsOrders from '../components/goods_order/GoodsOrders.vue'
// import Resport from '../components/resport/Resport.vue'
const GoodsOrders = () => import(/* webpackChunkName: "GoodsOrders"*/ '../components/goods_order/GoodsOrders.vue')
const Resport = () => import(/* webpackChunkName: "Resport"*/ '../components/resport/Resport.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/lorR/login' },
  {
    path: '/lorR',
    component: LorR,
    children:[
      { path:'login', component:Login},
      { path:'register', component:Register}
    ]
  },
  { path: '/mangerHome', component: MangerHome,
    redirect:'/mangerWelcome',   //路由重定向
    children:[
      { path:'/mangerWelcome', component:MangerWelcome},
      { path:'/users', component:UserManger},   //用户管理路径
      { path:'/rights', component:RightManger},    //权限管理路径
      { path:'/roles', component:RolesManger},    //角色管理路径
      { path:'/categories', component:GoodsCate},    //商品分类路径
      { path:'/params', component:GoodsParams},    //分类参数路径 
      { path:'/goods', component:GoodsList},    //商品列表路径 
      { path:'/goods/add', component:GoodAdd},    //添加商品页面路径
      { path:'/orders', component:GoodsOrders},    //订单管理路径
      { path:'/reports', component:Resport},    //订单管理路径
       
    ]
  }
  
]

const router = new VueRouter({
  routes
})

// 路由守卫 判断是否有权限访问指定路由
router.beforeEach((to,from,next)=>{
  // 三个参数 to从哪个路由路径来的 from要到哪个路由路径去 next有权限就放行，可以跳转
  if(to.path === '/lorR/login') return next()  //如果成功登入就可以跳转
  const tokenstr = window.sessionStorage.getItem('token')   //获取用户成功登入后浏览器生成的token
  if(!tokenstr) return next('/lorR/login')  //如果获取失败则说明没有登入成功 路由跳转到登入界面
  next()
})

export default router
